'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
	const collection = db.collection('db_user')
	const {
		openid,
		platform
	} = event
	let searchResult
	try {
		searchResult = await collection.where({
			openid,
			platform
		}).get()
		return {
			code: 0,
			data: {
				id: searchResult.data[0]._id
			}
		}
	} catch (err) {
		try {
			if (searchResult.affectedDocs === 0) {
				const res = await collection.add(event)
				return {
					code: 0,
					data: res
				}
			}
		} catch (err) {
			return {
				code: -1,
				msg: '服务器错误'
			}
		}
	}
};
